﻿using Global.Globalcs;
using System.Data;

namespace PHZPNW.API
{
    public class qxgl
    {
        MSql msql = new MSql();
        public qxgl()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        //获取json格式菜单
        public string jscd(string jsid, string bmid)
        {
            string jsinf = "";
            jsinf = "[";
            jsinf = xhdy("0", jsinf, "s_menu", "fmenu", jsid, "s_part", "id", "pagename", bmid);
            jsinf = jsinf + "]";
            return jsinf;
        }
        //处理部门页面权限
        public void bmpcg(string id, string pid, string uid, bool cz)
        {
            string sql = "";
            sql = "select * from l_role where roleid = '" + uid + "'";
            DataSet rinfo = msql.Selbeck(sql);
            string uname = rinfo.Tables[0].Rows[0]["rolename"].ToString();
            if (cz)
            {
                string cid = msql.getmaxid("id", "l_part", "where roleid = '" + uid + "'");
                sql = "insert into l_part(id,roleid,partid,pid)Values('" + cid + "','" + uid + "','" + id + "','" + pid + "')";
                msql.Datachange(sql);

                sql = "select * from s_menu where fmenu = '" + pid + "'";
                DataSet ds = msql.Selbeck(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        cid = msql.getmaxid("id", "l_part", "where roleid = '" + uid + "'");
                        pid = ds.Tables[0].Rows[i]["pageid"].ToString();
                        sql = "insert into l_part(id,roleid,partid,pid)Values('" + cid + "','" + uid + "','" + id + "','" + pid + "')";
                        msql.Datachange(sql);

                        string rjym = ds.Tables[0].Rows[i]["pageid"].ToString();
                        sql = "select * from s_menu where fmenu = '" + rjym + "'";
                        DataSet dt = msql.Selbeck(sql);
                        if (dt.Tables[0].Rows.Count > 0)
                        {
                            for (int m = 0; m < dt.Tables[0].Rows.Count; m++)
                            {
                                cid = msql.getmaxid("id", "l_role", "where roleid = '" + uid + "'");
                                pid = dt.Tables[0].Rows[m]["pageid"].ToString();
                                sql = "insert into l_part(id,roleid,partid,pid)Values('" + cid + "','" + uid + "','" + id + "','" + pid + "')";
                                msql.Datachange(sql);
                            }
                        }
                    }
                }
                else
                {
                    cid = msql.getmaxid("id", "l_role", "where roleid = '" + uid + "'");
                    sql = "insert into l_part(id,roleid,partid,pid)Values('" + cid + "','" + uid + "','" + id + "','" + pid + "')";
                    msql.Datachange(sql);
                }
            }
            else
            {
                sql = "delete l_part  where roleid = '" + uid + "' and pid = '" + pid + "' and partid = '" + id + "' ";
                msql.Datachange(sql);
                sql = "select * from s_menu where fmenu = '" + id + "'";
                DataSet ds = msql.Selbeck(sql);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string rjym = ds.Tables[0].Rows[i]["pageid"].ToString();
                    sql = "delete  from l_part where roleid = '" + uid + "' and pid = '" + rjym + "' and partid = '" + id + "'";
                    msql.Datachange(sql);
                    sql = "select * from s_menu where fmenu = '" + rjym + "'";
                    DataSet dt = msql.Selbeck(sql);
                    for (int m = 0; m < dt.Tables[0].Rows.Count; m++)
                    {
                        string sjym = dt.Tables[0].Rows[i]["pageid"].ToString();
                        sql = "delete  from l_part where roleid = '" + uid + "' and pid = '" + sjym + "' and partid = '" + id + "'";
                        msql.Datachange(sql);
                    }
                }
            }
        }
        public void pcl(string id, string pid, string uid, bool cz)
        {
            string sql = "";
            DataSet ds;
            sql = "select * from s_menu where pageid='" + pid + "'  order by id";
            DataSet tot = msql.Selbeck(sql);
            for (int i = 0; i < tot.Tables[0].Rows.Count; i++)
            {
                string pageid = tot.Tables[0].Rows[i]["pageid"].ToString();
                //bmpcg(id, pageid, uid, cz);
                sql = "select * from l_part where roleid = '" + uid + "' and pid = '" + pageid + "' and partid = '" + id + "' ";
                ds = msql.Selbeck(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (!cz)
                    {
                        sql = "delete l_part  where roleid = '" + uid + "' and pid = '" + pageid + "' and partid = '" + id + "' ";
                        msql.Datachange(sql);
                    }
                }
                else
                {
                    if (cz)
                    {
                        string cid = msql.getmaxid("id", "l_part", "where roleid = '" + uid + "'");
                        sql = "insert into l_part(id,roleid,partid,pid)Values('" + cid + "','" + uid + "','" + id + "','" + pageid + "')";
                        msql.Datachange(sql);
                    }

                }
            }
        }




        //循环赋值
        protected string xhdy(string sjbm, string crz, string Mtab, string Mid, string uid, string cxb, string zd1, string zd2, string bm)
        {
            string fh = "";
            fh = crz;
            string sql = "";
            DataSet ds;
            sql = "select " + zd1 + "," + zd2 + " from " + Mtab + " where " + Mid + " = '" + sjbm + "' order by yxj";
            ds = msql.Selbeck(sql);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string bmid = ds.Tables[0].Rows[i][0].ToString();
                string bmname = ds.Tables[0].Rows[i][1].ToString();
                //查询是否有下级菜单
                sql = "select * from " + Mtab + " where " + Mid + " = '" + bmid + "' order by yxj";
                if (msql.Selcount(sql) < 1)
                {
                    sql = "select * from " + cxb + " where pid = '" + bmid + "' and roleid = '" + uid + "' and partid = '" + bm + "'";
                    if (msql.Selcount(sql) > 0)
                    {
                        fh = fh + "{\"id\": \"" + bmid + "\",\"text\": \"" + bmname + "\",\"checked\":\"true\",\"children\": [";
                    }
                    else
                    {
                        fh = fh + "{\"id\": \"" + bmid + "\",\"text\": \"" + bmname + "\",\"children\": [";
                    }
                }
                else
                {
                    fh = fh + "{\"id\": \"" + bmid + "\",\"text\": \"" + bmname + "\",\"children\": [";
                }

                //生成下级菜单
                fh = xhdy(bmid, fh, Mtab, Mid, uid, cxb, zd1, zd2, bm);
                if (i == ds.Tables[0].Rows.Count - 1)
                {
                    fh = fh + "]}";
                }
                else
                {
                    fh = fh + "]},";
                }
            }
            return fh;
        }
    }
}